package com.neudu.ssrs_yyzxservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.neudu.ssrs_yyzxservice.common.JSONReturn;
import com.neudu.ssrs_yyzxservice.mapper.FoodMapper;
import com.neudu.ssrs_yyzxservice.pojo.Food;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
public class FoodController {

    @Autowired
    private FoodMapper foodMapper;

    @Autowired
    private JSONReturn jsonReturn;

    // 查询所有膳食记录（分页）
    @PostMapping("/get_all_foods")
    public String getAllFoods(@RequestBody Map<String, Object> params) {
        try {
            int pageNum = (Integer) params.get("pageNum");
            int pageSize = (Integer) params.get("pageSize");
            PageHelper.startPage(pageNum, pageSize);
            List<Food> foodList = foodMapper.selectList(null);
            PageInfo<Food> pageInfoResult = new PageInfo<>(foodList);
            return jsonReturn.returnSuccess(pageInfoResult);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据膳食名称和星期查询膳食记录
    @PostMapping("/get_foods_by_name")
    public String getFoodsByName(@RequestBody Map<String, String> param) {
        try {
            LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(param.get("foodname"))) {
                queryWrapper.like(Food::getFoodname, param.get("foodname"));
            }
            if (!ObjectUtils.isEmpty(param.get("supplyDate"))) {
                queryWrapper.eq(Food::getSupplyDate, param.get("supplyDate"));
            }
            List<Food> foodList = foodMapper.selectList(queryWrapper);
            return jsonReturn.returnSuccess(foodList);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    @PostMapping("/get_foods_by_weekday")
    public String getFoodsByWeekday(@RequestBody Map<String, String> param) {
        try {
            LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<>();
            if (!ObjectUtils.isEmpty(param.get("supplyDate")) && !param.get("supplyDate").equals("全部")) {
                queryWrapper.eq(Food::getSupplyDate, param.get("supplyDate"));
            }
            List<Food> foodList = foodMapper.selectList(queryWrapper);
            return jsonReturn.returnSuccess(foodList);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 添加一条膳食记录
    @PostMapping("/add_one_food")
    public String addFood(@RequestBody Food food) {
        try {
            int result = foodMapper.insert(food);
            if (result > 0) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 根据ID查询膳食记录
    @PostMapping("/get_food_by_id")
    public String getFoodById(@RequestParam("id") Integer id) {
        try {
            Food food = foodMapper.selectById(id);
            return jsonReturn.returnSuccess(food);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 修改膳食记录
    @PostMapping("/modify_food_by_id")
    public String modifyFoodById(@RequestBody Food food) {
        try {
            int result = foodMapper.updateById(food);
            if (result > 0) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("修改失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }

    // 删除膳食记录
    @PostMapping("/remove_food_by_id")
    public String removeFoodById(@RequestParam("id") Integer id) {
        try {
            int result = foodMapper.deleteById(id);
            if (result > 0) {
                return jsonReturn.returnSuccess();
            } else {
                return jsonReturn.returnError("删除失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return jsonReturn.returnError(e.getMessage());
        }
    }
}